package fivm.cross.platform.ficommon;

/**
 * Created by Administrator on 2016/5/13.
 */


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import fivm.cross.platform.ficommon.distinguish.GsonSimpleSerializable;

import java.lang.reflect.Type;

/**
 * 定义一个返回处理通用标准
 * */
public class HttpStandardMsg<H extends GsonSimpleSerializable, P extends GsonSimpleSerializable>
extends GsonSimpleSerializable {
    @Expose @SerializedName("Header")
    private H _header;
    @Expose @SerializedName("Payload")
    private P _payload;


    public HttpStandardMsg(H header, P payload){
        _header=  header;
        _payload=  payload;
    }
    public HttpStandardMsg(P payload){
        _header=  (H) MSG_Succ;
        _payload= payload;
    }
    
    public String toString(){
        return this.toJson();
    }




    public H get_header() {
        return _header;
    }

    public void set_header(H _header) {
        this._header = _header;
    }

    public P get_payload() {
        return _payload;
    }

    public void set_payload(P _payload) {
        this._payload = _payload;
    }

    public static final MsgHeader MSG_Succ=new MsgHeader("success","","");

    /**
     * 定义一个简化的http成功返回头
     * */
    public static class HTTPPayloadResp<P extends GsonSimpleSerializable> extends HttpStandardMsg<MsgHeader,P> {
        public HTTPPayloadResp(MsgHeader header,P payload) {
            super(header, payload);
        }
        public HTTPPayloadResp(P payload) {
            super(payload);
        }
        
        public boolean isErrMsg(){
            if(this.get_header().getStatus().equals("error")){
                return true;
            }
            if(this.get_payload()==null){
            	return true;
            }
            return false;
        }


        /**
         * 生成错误信息
         * @param action    错误的行为
         * @param msg       错误的基本信息
         * @param payLoad   错误的具体内容
         * */
        public static String genErrMsg(String action, String msg, GsonSimpleSerializable payLoad){
            return new HttpStandardMsg(new MsgHeader("error",action,msg),payLoad).toString();
        }
        public static String genErrMsg(String action, String msg){
            return genErrMsg(action,msg,null);
        }
        public static String genErrMsg(String msg){
            return genErrMsg("",msg,null);
        }

        /**
         * 生成成功的http消息，P为要生成的对象
         * */
        public static <P extends GsonSimpleSerializable> HTTPPayloadResp<P> genSuccObj(P payload){
            return new HTTPPayloadResp(payload);
        }
        public static <P extends GsonSimpleSerializable> String genSuccMsg(P payload){
            return genSuccObj(payload).toString();
        }
    }

    /**
     * 定义一个http消息头格式
     * */
    public static class MsgHeader extends GsonSimpleSerializable{
        @Expose @SerializedName("Status")
        private String status;  //状态，比如成功或者错误
        @Expose @SerializedName("Action")
        private String action;  //操作action
        @Expose @SerializedName("ExtraMsg")
        private String extraMsg;   //额外的信息，比如出错，把错误信息放在里
        public MsgHeader(){
            status ="";
            action ="";
            extraMsg ="";
        }
        public MsgHeader(String status,String action,String extraMsg){
            this.status =status;
            this.action =action;
            this.extraMsg =extraMsg;
        }

        public boolean isSucess(){
            if(this.status==null){
                return false;
            }
            if(this.status.equals("success")){
                return true;
            }
            return false;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getAction() {
            return action;
        }

        public void setAction(String action) {
            this.action = action;
        }

        public String getExtraMsg() {
            return extraMsg;
        }

        public void setExtraMsg(String extraMsg) {
            this.extraMsg = extraMsg;
        }
    }

}